#include "ATSPInstance.h"

ATSPInstance::ATSPInstance()
:Instance() {
	C = 0;
	distance = 0;
}

ATSPInstance::~ATSPInstance() {
	if (distance != NULL)
		delete [] distance;
}

bool ATSPInstance::load( const char *name, Problem *problem ) throw (NotFound) {
	Instance::load(name, problem);

	Input in(getPath());

	in.read(C);

	distance = new int[C*C];
	in.read( distance, C*C );

	ready = true;
	return true;
}

int ATSPInstance::getSolSize() throw (NotReady) {
	if (!ready) throw NotReady("ATSP Instance");
	return C;
}

int ATSPInstance::getSolCols() throw (NotReady) {
	if (!ready) throw NotReady("ATSP Instance");
	return C;
}

int ATSPInstance::getSolRows() throw (NotReady) {
	if (!ready) throw NotReady("ATSP Instance");
	return 1;
}

int ATSPInstance::getDistance( int i, int j ) throw (NotReady) {
	if (!ready) throw NotReady("ATSP Instance");
	return distance[ map(i,j,C) ];
}

void ATSPInstance::print() throw (NotReady) {
	if (!ready) throw NotReady("ATSP Instance");

	cout << "Instance: " << name << endl;
	cout << "Cities: " << C << endl << endl;
	cout << "Distances:" << endl;
	for (int f = 0; f < C; f++) {
		for (int t = 0; t < C; t++)
			cout << setw(4) << distance[Util::map(f,t,C)];
		cout << endl;
	}
	cout << endl;
}

void ATSPInstance::write( const char *file ) throw (NotReady) {
	if (!ready) throw NotReady("ATSP Instance");
}